package com.handong.handserialprogram.ui;

import android.content.DialogInterface;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.utils.AppUtil;

import me.shaohui.bottomdialog.BottomDialog;


/**
 * 弹出输入对话框。
 * <p>
 * Created by userKey on 2017/3/14.
 */

public class UpdateDistanceDialog extends BottomDialog {

    private EditText mInputDistance;
    private Button mBtnSure;
    private BtnSureListener mSureListener;

    public UpdateDistanceDialog() {
    }

    public static UpdateDistanceDialog createDialog(FragmentManager manager, BtnSureListener listener) {
        UpdateDistanceDialog distanceDialog = new UpdateDistanceDialog();
        distanceDialog.mSureListener = listener;
        distanceDialog.setFragmentManager(manager)
                .setLayoutRes(R.layout.dialog_update_distance)
                .setDimAmount(0.3f)
                .setTag("修改距离");
        return distanceDialog;
    }

    @Override
    public void bindView(View view) {
        mInputDistance = (EditText) view.findViewById(R.id.inputNewRingDistance);
        mBtnSure = (Button) view.findViewById(R.id.btnSureSetDistance);

        AppUtil.showSoftInput(mInputDistance);

        mBtnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dealInputData(mInputDistance.getText().toString().trim());
            }
        });
        //getDialog().getWindow().setFlags(ViewUtil.FLAG_HOMEKEY_DISPATCHED, ViewUtil.FLAG_HOMEKEY_DISPATCHED);
    }

    private void dealInputData(String data){
        if (TextUtils.isEmpty(data)) {
            AppUtil.showShortToast(getContext(), "请输入设定距离");
            return;
        }
        int newSetDistance = Integer.parseInt(data);
        if (newSetDistance == 0) {
            AppUtil.showShortToast(getContext(), "设定距离不能为零");
            return;
        }
        if (newSetDistance > 25) {
            AppUtil.showShortToast(getContext(), "设定距离不能超过25m");
            return;
        }
        String str = Integer.toHexString(newSetDistance).toUpperCase();
        if (newSetDistance < 16) {
            str = "0" + str;
        }
        AppUtil.hideSoftInput(getContext(), getDialog().getWindow());
        mSureListener.btnSure(str);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (mInputDistance != null) {
            mInputDistance.setCursorVisible(false);
            mInputDistance.setText("");
        }
        super.onDismiss(dialog);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public void setBtnSure(BtnSureListener listener) {
        mSureListener = listener;
    }

    public interface BtnSureListener {
        void btnSure(String s);
    }
}